﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;

namespace DYSales
{
    public partial class MainPage : UserControl
    {
        LoginWin lw;
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
            hlb_销售.Click += new RoutedEventHandler(hlb_sell_Click);
            hlb_统计.Click += new RoutedEventHandler(hlb_sell_Click);
            hlb_仓库管理.Click += new RoutedEventHandler(hlb_sell_Click);
            hlb_系统设置.Click += new RoutedEventHandler(hlb_sell_Click);

            hlb_仓库管理.Click += new RoutedEventHandler(hlb_仓库管理_Click);
            hlb_系统设置.Click += new RoutedEventHandler(hlb_系统设置_Click);
            hlb_销售.Click += new RoutedEventHandler(hlb_销售_Click);
            hlb_统计.Click += new RoutedEventHandler(hlb_统计_Click);
            hlb_修改密码.Click += new RoutedEventHandler(hlb_修改密码_Click);
            hlb_锁屏.Click += new RoutedEventHandler(hlb_锁屏_Click);
            hlb_安全退出.Click += new RoutedEventHandler(hlb_安全退出_Click);
            hlb_交班.Click += new RoutedEventHandler(hlb_交班_Click);

            KeyDown += new KeyEventHandler(MainPage_KeyDown);
        }

        void hlb_交班_Click(object sender, RoutedEventArgs e)
        {
            var totalWin = new Views.cwin_class();
            totalWin.Closed += new EventHandler(totalWin_Closed);
            totalWin.Show();
        }

        void totalWin_Closed(object sender, EventArgs e)
        {
            var cwin = sender as Views.cwin_class;
            cwin.Closed -= new EventHandler(totalWin_Closed);
            if (cwin.DialogResult.Value)
            {
                hlb_安全退出_Click(null, null);
            }
        }

        void hlb_安全退出_Click(object sender, RoutedEventArgs e)
        {
            var box = other.GetMessageBox();
            box.Title = "东雅进销存提示";
            box.tbk_text.Text = "确定是退出系统吗？";
            box.Show();
            box.Closed += new EventHandler(box_Closed);
        }

        void box_Closed(object sender, EventArgs e)
        {
            var box = sender as Views.cwin_Question;
            box.Closed -=new EventHandler(box_Closed);
            if (box.DialogResult.Value)
            {
                WebContext.Current.Authentication.Logout((lo) =>
                {
                    dm.refreshApp();
                }, null);
            }
        }

        void hlb_锁屏_Click(object sender, RoutedEventArgs e)
        {
            var lck_win = new Lock_Win();
            lck_win.tbk_text.Text = "当前用户：" + WebContext.Current.User.Name;
            lck_win.tb_pwd.Password = string.Empty;
            lck_win.Show();
        }

        void hlb_修改密码_Click(object sender, RoutedEventArgs e)
        {
            var pwdWin = new Views.sys.cwin_updatePwd();
            pwdWin.Show();
        }

        void MainPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Insert)
            {
                if (ContentFrame.Content.GetType().Equals(typeof(Views.sell.main_page)))
                {
                    var sellui = ContentFrame.Content as Views.sell.main_page;
                    if (sellui != null)
                    {
                        sellui.bt_开单_Click(null, null);
                    }
                }
            }
        }

        void hlb_统计_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new Uri("/Views/total/main_page.xaml", UriKind.Relative));
        }

        void hlb_销售_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new Uri("/Views/sell/main_page.xaml", UriKind.Relative));
        }

        void hlb_系统设置_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new Uri("/Views/sys/main_page.xaml", UriKind.Relative));
        }

        void hlb_仓库管理_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(new Uri("/Views/Stock/main_page.xaml", UriKind.Relative));
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.Current.IsRunningOutOfBrowser)
            {
                App.Current.MainWindow.WindowState = WindowState.Maximized;
            }
            if (!WebContext.Current.User.IsAuthenticated)
            {
                lw = new LoginWin();
                lw.Closed += new EventHandler(lw_Closed);
                lw.Show();
            }
        }

        void lw_Closed(object sender, EventArgs e)
        {
            lw.Closed -= new EventHandler(lw_Closed);
            if (lw.DialogResult.Value)
            {
                if (WebContext.Current.User.Roles.First() == em_角色.收银员.ToString())
                {
                    hlb_销售_Click(null, null);
                    hlb_仓库管理.IsEnabled = false;
                    hlb_统计.IsEnabled = false;
                    hlb_系统设置.IsEnabled = false;
                    hlb_销售.IsEnabled = true;
                    return;
                }
                if (WebContext.Current.User.Roles.First() == em_角色.财务员.ToString())
                {
                    hlb_仓库管理.IsEnabled = false;
                    hlb_统计.IsEnabled = true;
                    hlb_系统设置.IsEnabled = false;
                    hlb_销售.IsEnabled = false;
                    return;
                }
                if (WebContext.Current.User.Roles.First() == em_角色.仓库管理员.ToString())
                {
                    hlb_仓库管理.IsEnabled = true;
                    hlb_统计.IsEnabled = false;
                    hlb_系统设置.IsEnabled = false;
                    hlb_销售.IsEnabled = false;
                    return;
                }
                if (WebContext.Current.User.Roles.First() == em_角色.店长.ToString())
                {
                    hlb_仓库管理.IsEnabled = true;
                    hlb_统计.IsEnabled = true;
                    hlb_系统设置.IsEnabled = true;
                    hlb_销售.IsEnabled = true;
                }

            }
        }

        void hlb_sell_Click(object sender, RoutedEventArgs e)
        {
            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null)
                {
                    if (hb.Equals((HyperlinkButton)sender))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
        }

        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {

        }

    }
}